package com.github.ecbp.store.service.controller.front;

import com.github.ecbp.common.api.CommonResult;
import com.github.ecbp.common.data.builder.CommonPageBuilder;
import com.github.ecbp.common.data.controller.BaseController;
import com.github.ecbp.common.data.utils.TransformUtils;
import com.github.ecbp.common.req.BaseIdDTO;
import com.github.ecbp.common.req.BaseIdListDTO;
import com.github.ecbp.common.req.BaseIdStatusDTO;
import com.github.ecbp.common.utils.Asserts;
import com.github.ecbp.store.service.common.StoreCodeAndMessageEnum;
import com.github.ecbp.store.service.dto.ProductCategoryDTO;
import com.github.ecbp.store.service.dto.ProductCategoryQueryDTO;
import com.github.ecbp.store.service.entity.ProductCategory;
import com.github.ecbp.store.service.service.ProductCategoryService;
import com.github.ecbp.store.service.vo.ProductCategoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.github.ecbp.common.constant.CacheKeyConstant.PROJECT_NAME;

/**
 * @Author Created by Alex·He on 2020/4/28 15:56
 * @Version 1.0
 * @Description 商品分类表(ProductCategory)表控制层
 */
@Api(tags = "商品分类管理")
@RequestMapping("/api/" + PROJECT_NAME + "/productCategory")
@RestController
public class ProductCategoryController extends BaseController {
    @Autowired
    private ProductCategoryService service;

    @ApiOperation(value = "新增商品分类")
    @PostMapping(value = "/create")
    public CommonResult create(@Validated @RequestBody ProductCategoryDTO param) {
        service.save(param);
        return CommonResult.succeed();
    }

    @ApiOperation(value = "根据ID删除商品分类")
    @PostMapping(value = "/delete")
    public CommonResult delete(@Validated(BaseIdDTO.NotNullId.class) @RequestBody BaseIdDTO param) {
        Asserts.isTrue(service.removeById(param.getId()), StoreCodeAndMessageEnum.PRODUCT_CATEGORY_DELETE_ERROR);
        return CommonResult.succeed();
    }

    @ApiOperation(value = "批量删除商品分类")
    @PostMapping("/deleteByBatch")
    public CommonResult deleteByBatch(@Validated @RequestBody BaseIdListDTO param) {
        Asserts.isTrue(service.removeByIds(param.getIds()), StoreCodeAndMessageEnum.PRODUCT_CATEGORY_DELETE_ERROR);
        return CommonResult.succeed();
    }

    @ApiOperation(value = "修改商品分类")
    @PostMapping(value = "/update")
    public CommonResult update(@Validated(BaseIdDTO.NotNullId.class) @RequestBody ProductCategoryDTO param) {
        service.updateById(param);
        return CommonResult.succeed();
    }

    @ApiOperation(value = "修改商品分类显示状态")
    @PostMapping(value = "/updateShowStatus")
    public CommonResult updateShowStatus(@Validated @RequestBody BaseIdStatusDTO param) {
        return CommonResult.succeed(service.updateShowStatus(param.getId(), param.getStatus()));
    }

    @ApiOperation(value = "修改商品分类的导航栏显示状态")
    @PostMapping(value = "/updateNavStatus")
    public CommonResult updateNavStatus(@Validated @RequestBody BaseIdStatusDTO param) {
        return CommonResult.succeed(service.updateNavStatus(param.getId(), param.getStatus()));
    }

    @ApiOperation(value = "根据ID获取商品分类详情", response = ProductCategoryVO.class)
    @PostMapping(value = "/info")
    public CommonResult getItem(@Validated(BaseIdDTO.NotNullId.class) @RequestBody BaseIdDTO param) {
        return CommonResult.succeed(TransformUtils.copy(service.getById(param.getId()), ProductCategoryVO.class));
    }

    @ApiOperation(value = "获取全部商品分类列表")
    @PostMapping("/listAll")
    public CommonResult listAll() {
        return CommonResult.succeed(service.list());
    }

    @ApiOperation(value = "分页列表-商品分类", response = ProductCategoryVO.class)
    @PostMapping(value = "/listAllPage")
    public CommonResult listAllPage(@Validated @RequestBody ProductCategoryQueryDTO queryDTO) {
        return CommonResult.succeed(CommonPageBuilder.build(service.page(TransformUtils.copy(queryDTO, ProductCategory.class)),
                ProductCategoryVO.class));
    }

}
